package com.example.administrator.newxlmedical;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseActivity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import adapters.CountSpinnerAdapter;
import adapters.ErrorGoodsAdapter;
import beans.GoodsDetailInfoBean;
import beans.OperateGoodsDetailsBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import constants.Constants;
import interfaces.MyItemClickListener;
import utils.CustomSharedPreferencePath;
import utils.InventorySortUtils;
import utils.TimeUtil;
import utils.ViewUtil;

public class ErrorGoodsActivity extends BaseActivity {

    @BindView(R.id.error_goods_home)
    ImageView errorGoodsHome;
    @BindView(R.id.error_goods_spinner)
    Spinner mSpinner;
    @BindView(R.id.error_goods_number)
    TextView tvCabinet;
    @BindView(R.id.iv_error_goods_cabinet_sort)
    ImageView ivCabinetSort;
    @BindView(R.id.rl_error_goods_cabinet_sort)
    RelativeLayout rlErrorGoodsCabinetSort;
    @BindView(R.id.error_goods_goods)
    TextView tvGoods;
    @BindView(R.id.iv_error_goods_goods_sort)
    ImageView ivGoodsSort;
    @BindView(R.id.rl_error_goods_goods_sort)
    RelativeLayout rlErrorGoodsGoodsSort;
    @BindView(R.id.error_goods_size)
    TextView tvSpecification;
    @BindView(R.id.iv_error_goods_specification_sort)
    ImageView ivSpecificationSort;
    @BindView(R.id.rl_error_goods_specification_sort)
    RelativeLayout rlErrorGoodsSpecificationSort;
    @BindView(R.id.error_goods_supplier)
    TextView tvSupplier;
    @BindView(R.id.iv_error_goods_supplier_sort)
    ImageView ivSupplierSort;
    @BindView(R.id.rl_error_goods_supplier_sort)
    RelativeLayout rlErrorGoodsSupplierSort;
    @BindView(R.id.error_goods_count)
    TextView tvNumber;
    @BindView(R.id.iv_error_goods_number_sort)
    ImageView ivNumberSort;
    @BindView(R.id.rl_error_goods_number_sort)
    RelativeLayout rlErrorGoodsNumberSort;
    @BindView(R.id.error_goods_lv)
    RecyclerView recyclerView;
    @BindView(R.id.fl_error_goods)
    FrameLayout flErrorGoods;
    @BindView(R.id.error_goods_ll)
    LinearLayout errorGoodsLl;
    @BindView(R.id.error_goods_statu)
    TextView tvStatus;
    @BindView(R.id.iv_error_goods_statu_sort)
    ImageView ivStatusSort;
    @BindView(R.id.rl_error_goods_statu_sort)
    RelativeLayout rlStatusSort;

    private List<OperateGoodsDetailsBean.DataBean> list = new ArrayList<>();
    private List<OperateGoodsDetailsBean.DataBean> commonList = new ArrayList<>();
    private String[] spinnerData = {"全部", "1", "2", "3", "4", "5"};
    private CountSpinnerAdapter mAdapter;
    private int mPosition;
    private ErrorGoodsAdapter adapter;

    private boolean isSortGoods, isSortSpecification, isSortSupplier, isSortNumber, isSortCabinet, isSortStatus;
    private int sortGoods, sortSpecification, sortSupplier, sortNumber, sortCabinet, sortStatus;
    private boolean isFrist = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_error_goods);
        ButterKnife.bind(this);

    }

    @Override
    public void initView() {
        super.initView();

        showLoadingView(flErrorGoods);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new ErrorGoodsAdapter(this, list);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);

        Map<String, OperateGoodsDetailsBean.DataBean> recordList = (Map<String, OperateGoodsDetailsBean.DataBean>) CustomSharedPreferencePath.getData(this, Constants.RECORD_ERROR_GOODS, null);

        if (recordList != null && recordList.size() != 0) {
            Set<Map.Entry<String, OperateGoodsDetailsBean.DataBean>> entries = recordList.entrySet();
            Iterator<Map.Entry<String, OperateGoodsDetailsBean.DataBean>> iterator = entries.iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, OperateGoodsDetailsBean.DataBean> next = iterator.next();
                OperateGoodsDetailsBean.DataBean value = next.getValue();
                if (value.getNumber() > 0) {
                    list.add(value);
                }
            }

            if (list.size() != 0) {
                commonList.addAll(list);
                adapter.notifyDataSetChanged();
            } else {
                showEmptyView(flErrorGoods);
            }
            hideLoadingView();
        } else {
            hideLoadingView();
            showEmptyView(flErrorGoods);
        }

        mAdapter = new CountSpinnerAdapter(this, R.layout.time_out_spinner_layout, spinnerData);

        mAdapter.setDropDownViewResource(R.layout.spinner_bg);

        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mPosition = position;
                mAdapter.setSelectPosition(mPosition);
                if (!isFrist) {
                    sort(0);
                    recyclerView.setVisibility(View.GONE);
                    selectCabinet(position);
                }else {
                    isFrist = false;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        mSpinner.setAdapter(mAdapter);
    }

    private void selectCabinet(int position) {
        showLoadingView(flErrorGoods);
        if (position == 0) {
            list.clear();
            list.addAll(commonList);
            hideEmptyView();
            recyclerView.setVisibility(View.VISIBLE);
        } else {
            List<OperateGoodsDetailsBean.DataBean> cabinetList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                OperateGoodsDetailsBean.DataBean dataBean = list.get(i);
                String selectCabinet = dataBean.getSelectCabinet();
                if (selectCabinet.equals(position + "")) {
                    cabinetList.add(dataBean);
                }
            }

            if (cabinetList.size() != 0) {
                list.clear();
                list.addAll(cabinetList);
                hideEmptyView();
                recyclerView.setVisibility(View.VISIBLE);
            } else {
                showEmptyView(flErrorGoods);
            }
        }

        hideLoadingView();
    }

    @Override
    protected void setListener() {
        super.setListener();
        adapter.setItemClickListener(new MyItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                OperateGoodsDetailsBean.DataBean bean = list.get(position);
                GoodsDetailInfoBean infoBean = new GoodsDetailInfoBean();
                infoBean.setName(bean.getGoodName());
                infoBean.setBarCode(bean.getBarCodeList());
                infoBean.setSpecification(bean.getSpecification());
                infoBean.setSupplier(bean.getManufacturer());
                infoBean.setTime(TimeUtil.getNowTime(bean.getErrorRecordTime()).substring(0,19));
                infoBean.setUnit(bean.getGoodsUnit());
                infoBean.setNumber(bean.getNumber());
                infoBean.setStatus(bean.getStatus());
                infoBean.setTimeName("入柜时间");
                infoBean.setTitle("异常物资");
                infoBean.setTitleContent("物品详情");

                Bundle bundle = new Bundle();
                bundle.putSerializable(Constants.GOODS_DETIAL_INFO,infoBean);
                Intent intent = new Intent(ErrorGoodsActivity.this, GoodsDetialActivity.class);
                intent.putExtra("bundle", bundle);
                startActivity(intent);
            }
        });
    }

    @OnClick({R.id.error_goods_home, R.id.rl_error_goods_cabinet_sort,
            R.id.rl_error_goods_goods_sort, R.id.rl_error_goods_specification_sort,
            R.id.rl_error_goods_supplier_sort, R.id.rl_error_goods_number_sort,
            R.id.rl_error_goods_statu_sort})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.error_goods_home:
                finish();
                break;
            case R.id.rl_error_goods_cabinet_sort:
                sort(5);
                break;
            case R.id.rl_error_goods_goods_sort:
                sort(1);
                break;
            case R.id.rl_error_goods_specification_sort:
                sort(3);
                break;
            case R.id.rl_error_goods_supplier_sort:
                sort(2);
                break;
            case R.id.rl_error_goods_number_sort:
                sort(4);
                break;
            case R.id.rl_error_goods_statu_sort:
                sort(6);
                break;
        }
    }

    private void sort(int type) {
        if (type == 1) { // 按物品名排序
            if (sortGoods != 2) {
                isSortGoods = !isSortGoods;
                sortGoods++;
                InventorySortUtils.sortGoodsName(list, isSortGoods);
            } else {
                clearSortDate();
            }
        } else {
            sortGoods = 0;
            isSortGoods = false;
        }

        if (type == 2) { // 按供应商排序
            if (sortSupplier != 2) {
                isSortSupplier = !isSortSupplier;
                sortSupplier++;
                InventorySortUtils.sortGoodsSupplier(list, isSortSupplier);
            } else {
                clearSortDate();
            }
        } else {
            sortSupplier = 0;
            isSortSupplier = false;
        }

        if (type == 3) {
            if (sortSpecification != 2) {
                sortSpecification++;
                isSortSpecification = !isSortSpecification;
                InventorySortUtils.sortGoodsSpecification(list, isSortSpecification);
            } else {
                clearSortDate();
            }
        } else {
            sortSpecification = 0;
            isSortSpecification = false;
        }

        if (type == 4) {
            if (sortNumber != 2) {
                sortNumber++;
                isSortNumber = !isSortNumber;
                InventorySortUtils.sortGoodsNumber(list, isSortNumber);
            } else {
                clearSortDate();
            }
        } else {
            sortNumber = 0;
            isSortNumber = false;
        }

        if (type == 5) {
            if (sortCabinet != 2) {
                sortCabinet++;
                isSortCabinet = !isSortCabinet;
                InventorySortUtils.sortCabinet(list, isSortCabinet);
            } else {
                clearSortDate();
            }
        }else {
            sortCabinet = 0;
            isSortCabinet = false;
        }

        if (type == 6) {
            if (sortStatus != 2) {
                sortStatus++;
                isSortStatus = !isSortStatus;
                InventorySortUtils.sortCabinet(list, isSortStatus);
            } else {
                clearSortDate();
            }
        }else {
            sortStatus = 0;
            isSortStatus = false;
        }

        ViewUtil.setSortColor(this, tvGoods, ivGoodsSort, sortGoods);
        ViewUtil.setSortColor(this, tvSupplier, ivSupplierSort, sortSupplier);
        ViewUtil.setSortColor(this, tvSpecification, ivSpecificationSort, sortSpecification);
        ViewUtil.setSortColor(this, tvNumber, ivNumberSort, sortNumber);
        ViewUtil.setSortColor(this, tvCabinet, ivCabinetSort, sortCabinet);
        ViewUtil.setSortColor(this, tvStatus,ivStatusSort,sortStatus);
        adapter.notifyDataSetChanged();

    }

    private void clearSortDate() {
        list.clear();
        list.addAll(commonList);

        sortGoods = 0;
        sortSpecification = 0;
        sortSupplier = 0;
        sortNumber = 0;
        sortStatus = 0;
        sortCabinet = 0;
    }

}
